package com.yehui.jdk.datetime;

import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;

/**
 * java.time.LocalDate：LocalDate是一个不可变的类，它表示默认格式(yyyy-MM-dd)的日期.
 * <br/>
 * 我们可以使用 now()方法得到当前时间，也可以提供输入年份、月份和日期的输入参数来创建一个LocalDate实例。
 * <br/>
 * 该类为now()方法提供了重载方法，我们可以传入ZoneId来获得指定时区的日期。
 * <br/>
 * 该类提供与java.sql.Date相同的功能.
 *
 * @author 业徽
 * @date 2019-02-15
 */
public class LocalDateFun {

    /**
     * 创建一个 LocalDate 对象
     */
    public void create() {
        // now date
        LocalDate today = LocalDate.now();
        System.out.println("Current Date=" + today);

        // 指定日期, 如果给的日期参数错误会抛 java.time.DateTimeException 异常
        LocalDate firstDayOf2019 = LocalDate.of(2019, Month.JANUARY, 1);
        System.out.println("Specific Date=" + firstDayOf2019);

        // 获取 2019 年第 100 天的日期
        LocalDate hundredDayOf2019 = LocalDate.ofYearDay(2019, 100);
        System.out.println("100th day of 2014=" + hundredDayOf2019);

        // 指定时区, 如果时区设置错误, 会抛 java.time.zone.ZoneRulesException 异常
        LocalDate todayOfTokyo = LocalDate.now(ZoneId.of("Asia/Tokyo"));
        System.out.println("Current Date in Japan=" + todayOfTokyo);

        // 从 1970-01-01 开始的 1200 天
        LocalDate dateFromBase = LocalDate.ofEpochDay(1200);
        System.out.println("365th day from base date= " + dateFromBase);

        LocalDate dateFromString = LocalDate.parse("2019-01-31");
        System.out.println("parse 2019-01-31: " + dateFromString);

    }

    /**
     * 查询
     */
    public void query() {

        LocalDate now = LocalDate.now();
        System.out.println("当前日期: " + now);

        System.out.println("当前年: " + now.getYear());
        System.out.println("当前月: " + now.getMonthValue());
        System.out.println("当前月的第几天: " + now.getDayOfMonth());
        System.out.println("星期: " + now.getDayOfWeek());
        System.out.println("星期对应的数字: " + now.getDayOfWeek()
            .getValue());
        System.out.println("当前年的第几天: " + now.getDayOfYear());
        System.out.println("当前年是否是闰年: " + now.isLeapYear());
    }

    /**
     * 比较
     */
    public void compare() {

        LocalDate date1 = LocalDate.parse("2019-01-31");
        LocalDate date2 = LocalDate.parse("2019-02-23");

        System.out.println(String.format("%s 早于 %s: %s", date1, date2, date1.isBefore(date2)));
        System.out.println(String.format("%s 早于 %s: %s", date1, date1, date1.isBefore(date1)));
        System.out.println(String.format("%s 晚于 %s: %s", date1, date2, date1.isAfter(date2)));
        System.out.println(String.format("%s 晚于 %s: %s", date1, date1, date1.isAfter(date1)));
        System.out.println(String.format("%s 和 %s 比较的结果: %s", date1, date2, date1.compareTo(date2)));
    }

    /**
     * 计算
     */
    public void calculate() {

        LocalDate source = LocalDate.parse("2019-01-31");

        LocalDate plusOneDay = source.plusDays(1);
        LocalDate plusOneMonth = source.plusMonths(1);
        LocalDate plusTwoMonth = source.plusMonths(2);
        LocalDate plusOneWeek = source.plusWeeks(1);
        LocalDate plusOneYear = source.plusYears(1);

        System.out.println(String.format(
            "source: %s, plusOneDay: %s, plusOneMonth: %s, , plusTwoMonth: %s, plusOneWeek: %s, plusOneYear: %s",
            source, plusOneDay, plusOneMonth, plusTwoMonth, plusOneWeek, plusOneYear));
    }

}
